// pages/manager/manager.js
import tips from '../../lib/tips';
var util = require('../../utils/util.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseDomain: app.globalData.baseDomain,
    tipsData: {
      title: '',
      isHidden: true
    },
    titleImgURL: "",
    contentImgURL: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  showToast(title, duartion) {
    const that = this;
    const tipsData = {
      title: title || '',
      duartion: duartion || 2000,
      isHidden: false
    };
    tips.toast(that.data.tipsData);
    that.setData({
      tipsData
    });
    setTimeout(() => {
      tipsData.isHidden = true;
      that.setData({
        tipsData
      });
    }, tipsData.duartion);
  },
  //上传图片
  onUploadImage: function(e) {
    var type = e.currentTarget.dataset.type;
    var that = this;
    //调用微信上传图片
    wx.chooseImage({
      sizeType: ['compressed'],
      count: 1,
      sourceType: ['album'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        if (tempFilePaths.length == 1){
          wx.uploadFile({
            url: app.globalData.apiDomain + '/upload/uploadGoodsTitleImg', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'user': 'test'
            },
            success(res) {
              var data = JSON.parse(res.data);
              console.log(data);
              if(type == 1){
                console.log(11111);
                that.setData({
                  titleImgURL: data.data
                });
              }else if(type == 2){
                console.log(222222);
                that.setData({
                  contentImgURL: data.data
                });
              }
            }
          })
        }
      }
    });
  },

  formSubmit:function(e){
    var url = app.globalData.apiDomain + '/api/goods/createGoods';
    var titleImgURL = this.data.titleImgURL;
    var contentImgURL = this.data.contentImgURL;
    wx.request({
      url: url,
      method: "POST",
      data: {
        goodsTitleImg: titleImgURL,
        goodsContentImg: contentImgURL
      },
      success: function (res) {
        wx.switchTab({
          url: '../index/index',
          complete: function () {
            wx.showToast({
              title: '发布成功',
              image: '../../images/correct_toast.png',
              duration: 2000,
            });
          }
        })
      },
      complete: function () {
      }
    });


  }
})